<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\Recommend;
use App\Models\Product;
use App\Models\Shop;
use App\Http\Controllers\Controller;
use App\Models\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ShopsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.shops'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.shops'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.shops'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.shops'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shop);
        $grid->model()->orderBy('sort', 'asc');
        $grid->id('Id');
        $grid->name(trans('admin.name'));
        $grid->email(trans('admin.email'))->display(function () {
            return $this->user->email;
        });
        $grid->face_picture(trans('admin.face_picture'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->begin_business_hours(trans('admin.begin_business_hours'))->editable('combodate', 'HH:mm');
        $grid->end_business_hours(trans('admin.end_business_hours'))->editable('combodate', 'HH:mm');
        $grid->free_time(trans('admin.free_time'))->display(function () {
            if ($this->begin_free_time && $this->end_free_time) {
                return date('Y-m-d H:i', strtotime($this->begin_free_time)) . '--' . date('Y-m-d H:i', strtotime($this->end_free_time));
            }
        });
        $grid->sort(trans('admin.sort'))->orderable();
        $grid->created_at(trans('admin.created_at'));
//        $grid->updated_at(trans('admin.updated_at'));
        $grid->actions(function ($action) {
            $action->prepend(new Recommend($action->getKey(), $action->row->recommended, 'Shop'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Shop::findOrFail($id));

        $show->id('Id');
        $show->name(trans('admin.name'));
        $show->email(trans('admin.email'));
        $show->begin_business_hours(trans('admin.begin_business_hours'));
        $show->end_business_hours(trans('admin.end_business_hours'));
        $show->created_at(trans('admin.created_at'));
        $show->products(trans('admin.product'), function ($data) {
            $data->setResource('/products');
            $data->title(trans('admin.title'));
            $data->subtitle(trans('admin.subtitle'));
            $data->image(trans('admin.image'))->lightbox(['width' => 50, 'height' => 50]);
            $data->content(trans('admin.content'))->limit(10);
            $data->service_time(trans('admin.service_time'));
            $data->contact_phone(trans('admin.contact_phone'));
            $data->price(trans('admin.price'));
//            $data->stock_amount(trans('admin.stock'));
            $data->view_amount(trans('admin.view_amount'));
            $data->sell_amount(trans('admin.sell_amount'));
            $data->filter(function ($filter) {
                $filter->column(1 / 3, function ($filter) {

                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('title', trans('admin.title'));
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->between('price', trans('admin.price'));
                });
            });
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Shop);

        $form->text('name', trans('admin.name'))->rules('required')->setLabelClass(['required']);
        $form->select('user_id', trans('admin.email'))->options(User::where('login_status', User::NORMAL)->where('role', User::SELLER)->pluck('email', 'id'));
        $form->image('face_picture', trans('admin.face_picture'))->removable();
        $form->text('begin_business_hours', trans('admin.begin_business_hours'));
        $form->text('end_business_hours', trans('admin.end_business_hours'));
        $form->datetimeRange('begin_free_time', 'end_free_time', trans('admin.free_time'));
//        $form->number('sort', trans('admin.sort'))->default(0);
        $form->radio('status', trans('admin.status'))->options(Shop::$STATUS_MAP)->default(Shop::PASS);
        $form->hasMany('products', trans('admin.product'), function (Form\NestedForm $form) {
            $form->text('title', trans('admin.title'))->rules('required')->setLabelClass(['required']);
            $form->text('subtitle', trans('admin.subtitle'));
            $form->image('image', trans('admin.image'));
            $form->decimal('price', trans('admin.price'))->default(0.00)->rules('required')->setLabelClass(['required']);
            $form->editor('content', trans('admin.content'));
//            $form->number('stock_amount', trans('admin.stock'))->default(0)->min(0);
            $form->number('view_amount', trans('admin.view_amount'))->default(0)->min(0);
            $form->number('sell_amount', trans('admin.sell_amount'))->default(0)->min(0);
//            $form->number('sort', trans('admin.sort'))->default(0)->min(0);
            $form->radio('is_show', trans('admin.show'))->options(Product::$IS_SHOW_MAP);
        });

        return $form;
    }
}
